package com.hmall.config;

import com.hmall.context.BaseContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Auth：Yang Zhen
 * @Desc：feign 请求过滤器，给 feign 请求添加用户的 ID
 * @Date：2023/4/20 19:50
 */
@Configuration
public class FeignInterceptorConfig {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long currentId = BaseContext.getCurrentId();
                // 在每一条请求头中添加 authorization
                requestTemplate.header("authorization", String.valueOf(currentId));// 替换原有对象
            }
        };
    }

}
